// person.js
class TaDiao {
    constructor(m1, m2, m3, m4, m5, m6, m7, m8) {
        // 静态group
        this.staticG = new THREE.Group();
        // 动态group
        this.animateG = new THREE.Group();
        // 吊钩和吊绳group
        this.hookLineGroup = new THREE.Group();
        // 吊钩group
        this.hookGroup = new THREE.Group();
        // 塔吊基础
        this.towerBase = m1;
        // 塔吊立柱组件
        this.towerComponent = m2;
        // 塔吊吊臂中段
        this.towerBoomMiddle = m3
        // 塔吊吊臂end
        this.towerBoomStart = m4
        // 塔吊吊臂start
        this.towerBoomEnd = m5
        // 塔吊吊臂吊钩
        this.towerBoomHook = m6
        // 塔吊吊臂吊绳
        this.towerBoomLine = m7
        // 吊绳起点
        this.startPoint = null
        // 吊绳终点
        this.endPoint = null
        // 吊绳geometry
        this.geometry = null
        this.lx = 0;
        this.ly = 0;
        this.al = 0;
        this.th = 0;
        // 吊臂中段个数
        this.dbMiddle = 0
        // 获取立柱组件个数
        this.lzBaseNum = 0
        // 圆geometry 
        this.circleGeometry = null
        // 阴影线起点
        this.startLinePoint = null
        // 阴影结束点
        this.endLinePoint = null
        // 阴影线geometry
        this.lineGeometry = null
        // 吊钩阴影起始点
        this.startHookShadowLine = null
        // 吊钩阴影结束点
        this.endHookShadowLine = null
        // 吊钩阴影geometry
        this.hookGeometry = null

    }
    // 参数: x，y，塔高，臂长
    loadMoudel(lx, ly, th, al) {
        this.lx = lx
        this.ly = ly;
        // 吊臂中段个数
        let dbMiddle = Math.floor(al / 5)
        console.log(al, dbMiddle)
        this.dbMiddle = dbMiddle
        // 获取立柱组件个数
        let lzBaseNum = Math.floor(th / 6.2)
        this.lzBaseNum = lzBaseNum

        // 塔吊基础
        let towerBase = this.towerBase.clone()
        towerBase.position.set(0, 1, 0)
        this.staticG.add(towerBase)

        // 塔吊立柱拼接
        for (let index = 0; index < lzBaseNum; index++) {
            let towerComponent = this.towerComponent.clone()
            towerComponent.position.set(0, index * 6.2, 0)
            this.staticG.add(towerComponent)
        }

        // 吊臂拼接个数
        for (let index = 0; index < dbMiddle; index++) {
            let towerBoomMiddle = this.towerBoomMiddle.clone()
            towerBoomMiddle.position.set(index * 5, lzBaseNum * 6.2, 0)
            this.animateG.add(towerBoomMiddle)

        }

        // 塔吊吊臂前段
        let towerBoomEnd = this.towerBoomEnd.clone()
        towerBoomEnd.position.set(dbMiddle * 5, lzBaseNum * 6.2, 0)
        this.animateG.add(towerBoomEnd)

        // 塔吊吊臂后段
        let towerBoomStart = this.towerBoomStart.clone()
        towerBoomStart.position.set(-9, lzBaseNum * 6.2, 0)
        towerBoomStart.scale.x = 3
        this.animateG.add(towerBoomStart)

        // 塔吊吊钩
        let towerBoomHook = this.towerBoomHook.clone()
        this.hookGroup.add(towerBoomHook)
        towerBoomHook.position.set(5, 35, 0)
        this.hookLineGroup.add(this.hookGroup)
        this.animateG.add(this.hookLineGroup)

        // 吊绳
        // 吊线起点和终点
        this.startPoint = new THREE.Vector3(5, lzBaseNum * 6.2, 0);
        this.endPoint = new THREE.Vector3(5, 35, 0);
        // 创建线几何体
        this.geometry = new THREE.BufferGeometry().setFromPoints([this.startPoint, this.endPoint]);
        
        // 创建材质
        let material = new THREE.LineBasicMaterial({ color: 0x000000 }); // 黑色绳索
        // 创建线对象
        let line = new THREE.Line(this.geometry, material);
        this.hookLineGroup.add(line)

        // 创建圆
        const circlegeometry = new THREE.TorusGeometry(dbMiddle * 5, 0.1, 32, 100); // 第一个参数是外圈半径，第二个参数是内圈半径，后面的参数是分段和圆环数
        const circlematerial = new THREE.MeshBasicMaterial({ color: 0x000000 }); // 绿色
        const circle = new THREE.Mesh(circlegeometry, circlematerial);
        circle.rotation.x = Math.PI / 2
        this.animateG.add(circle)

        // 创建一条阴影线
        this.startLinePoint = new THREE.Vector3(-9, 0, 0);
        this.endLinePoint = new THREE.Vector3(dbMiddle * 5, 0, 0);
        this.lineGeometry = new THREE.BufferGeometry().setFromPoints([this.startLinePoint, this.endLinePoint]);
        // 创建材质
        let lineMaterial = new THREE.LineBasicMaterial({ color: 0x000000 }); // 黑色绳索
        // 创建线对象
        let shadowLine = new THREE.Line(this.lineGeometry, lineMaterial);
        this.hookLineGroup.add(shadowLine)

        // 创建一个吊钩阴影
        this.startHookShadowLine = new THREE.Vector3(5, 0, 4);
        this.endHookShadowLine = new THREE.Vector3(5, 0, -4);
        this.hookGeometry = new THREE.BufferGeometry().setFromPoints([this.startHookShadowLine, this.endHookShadowLine]);
        let hookShadowLineMaterial = new THREE.LineBasicMaterial({ color: 0x000000 }); // 黑色阴影
        let hookShadowLine = new THREE.Line(this.hookGeometry, hookShadowLineMaterial);
        this.hookLineGroup.add(hookShadowLine)


        this.staticG.add(this.animateG)
        // 

        
    }

    draw(scene) {
        this.staticG.position.set(this.lx, 0, this.ly)
        scene.add(this.staticG)
    }

    updateLine () {
      // this.startPoint.x += 0.01
      // this.endPoint.x += 0.01
    //   console.log(this.endPoint.x, this.dbMiddle * 5)
      if (this.endPoint.x <= this.dbMiddle * 5) {
        this.endPoint.x += 0.05
        this.startPoint.x += 0.05
        this.hookGroup.position.x += 0.05
      }
      // this.towerBoomHook.position.x += 1
      // this.hookGroup.position.x += 0.01
      // console.log(this.hookGroup.position.y)
      if (this.endPoint.y <= this.lzBaseNum * 6.2) {
        this.endPoint.y += 0.05
        this.hookGroup.position.y += 0.05
      }
      this.geometry.setFromPoints([this.startPoint, this.endPoint]);
      this.geometry.attributes.position.needsUpdate = true;
    }
    updateShadowLine () {
      if (this.endPoint.x <= this.dbMiddle * 5) {
        this.startHookShadowLine.x += 0.05
        this.endHookShadowLine.x += 0.05
      }
      this.hookGeometry.setFromPoints([this.startHookShadowLine, this.endHookShadowLine]);
      this.hookGeometry.attributes.position.needsUpdate = true;
    }

    animate() {
        this.updateLine()
        this.updateShadowLine()
        // this.towerBoomHook.position.y += 10
        // this.hookLineGroup.position.x += 0.01
        this.animateG.rotation.y += 0.01
    }
}

// 导出这个类
export default TaDiao;